#include "rt_application.h"

#include "bsp_drv.h"

/* led thread */
static void led_thread_entry(void *parameter)
{
	while (1)
	{
		led2_blink();
		rt_thread_mdelay(500);
	}
}

/* adc thread */
static void adc_thread_entry(void *parameter)
{
	while (1)
	{
		rt_kprintf("adc sampling value: %d\n", adc_get_sample_value());
		rt_thread_mdelay(500);
	}
}

void rt_application_init()
{
	/* led thread */
	rt_thread_t led_thread = rt_thread_create("led", led_thread_entry, RT_NULL, 200, 20, 10);
	if (led_thread != RT_NULL)
	{
		rt_thread_startup(led_thread);
	}
	
	/* adc thread */
	rt_thread_t adc_thread = rt_thread_create("led", adc_thread_entry, RT_NULL, 200, 15, 10);
	if (led_thread != RT_NULL)
	{
		rt_thread_startup(adc_thread);
	}
}

void rtthread_startup(void)
{
    /* init board */
    rt_hw_board_init();

    /* init scheduler system */
    rt_system_scheduler_init();

    /* initialize timer */
    rt_system_timer_init();

#ifdef RT_USING_HEAP
    /* init memory system */
    rt_system_heap_init((void *)N32G45X_SRAM_START, (void *)N32G45X_SRAM_END);
#endif //RT_USING_HEAP

    /* init timer thread */
    rt_system_timer_thread_init();

    /* init application */
    rt_application_init();

    /* init idle thread */
    rt_thread_idle_init();

    /* start scheduler */
    rt_system_scheduler_start();

    /* never reach here */
    return;
}

#ifdef USE_FULL_ASSERT
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
{
	while (1)
	{
	}
}
#endif // USE_FULL_ASSERT


